<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
            v-model="queryParams.title"
            clearable
            placeholder="请输入标题"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="治疗类别" prop="treatmentCategory">
        <el-select v-model="queryParams.treatmentCategory" clearable placeholder="请选择治疗类别">
          <el-option
              v-for="dict in treatment_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['immunization:immunization:add']"
            icon="Plus"
            plain
            type="primary"
            @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            icon="Sort"
            plain
            type="info"
            @click="toggleExpandAll"
        >展开/折叠
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
        v-if="refreshTable"
        v-loading="loading"
        :data="immunizationList"
        :default-expand-all="isExpandAll"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        row-key="id"
    >
      <el-table-column align="center" label="标题" prop="title" width="130"/>
      <el-table-column align="center" label="治疗类别" prop="treatmentCategory" width="80">
        <template #default="scope">
          <dict-tag :options="treatment_category" :value="scope.row.treatmentCategory"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="鸡苗批次" prop="henBatch" width="80"/>
      <el-table-column align="center" label="日龄" prop="ageInDays" width="80"/>
      <el-table-column align="center" label="鸡群数量" prop="chickenCount" width="80"/>
      <el-table-column align="center" label="药品金额" prop="drugCost" width="80"/>
      <el-table-column align="center" label="操作时间" prop="operationTime" width="100">
        <template #default="scope">
          <span>{{ parseTime(scope.row.operationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="说明" prop="description" width="250"/>
      <el-table-column align="center" label="审核状态" prop="reviewStatus" width="80">
        <template #default="scope">
          <dict-tag :options="document_status" :value="scope.row.reviewStatus"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="审核时间" prop="reviewTime" width="200">
        <template #default="scope">
          <span>{{ parseTime(scope.row.reviewTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template #default="scope">
          <el-button v-hasPermi="['immunization:immunization:edit']" icon="Edit" link type="primary"
                     @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-hasPermi="['immunization:immunization:add']" icon="Plus" link type="primary"
                     @click="handleAdd(scope.row)">新增
          </el-button>
          <el-button v-hasPermi="['immunization:immunization:remove']" icon="Delete" link type="primary"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改治疗免疫对话框 -->
    <el-dialog v-model="open" :title="title" append-to-body width="600px">
      <el-form ref="immunizationRef" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="治疗类别" prop="treatmentCategory">
              <el-select v-model="form.treatmentCategory" placeholder="请选择治疗类别">
                <el-option
                    v-for="dict in treatment_category"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="鸡苗批次" prop="henBatch">
              <el-input v-model="form.henBatch" placeholder="请输入鸡苗批次"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="日龄" prop="ageInDays">
              <el-input v-model="form.ageInDays" placeholder="请输入日龄"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="鸡群数量" prop="chickenCount">
              <el-input v-model="form.chickenCount" placeholder="请输入鸡群数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="药品金额" prop="drugCost">
              <el-input v-model="form.drugCost" placeholder="请输入药品金额"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="操作时间" prop="operationTime">
              <el-date-picker v-model="form.operationTime"
                              clearable
                              placeholder="选择操作时间"
                              type="date"
                              value-format="YYYY-MM-DD">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="说明" prop="description">
              <el-input v-model="form.description" placeholder="请输入内容" type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="审核状态" prop="reviewStatus">
              <el-select v-model="form.reviewStatus" placeholder="请选择审核状态">
                <el-option
                    v-for="dict in document_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="审核时间" prop="reviewTime">
              <el-date-picker v-model="form.reviewTime"
                              clearable
                              placeholder="选择审核时间"
                              type="date"
                              value-format="YYYY-MM-DD">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script name="Immunization" setup>
import {
  addImmunization,
  delImmunization,
  getImmunization,
  listImmunization,
  updateImmunization
} from "@/api/immunization/immunization";

const {proxy} = getCurrentInstance();
const {document_status, treatment_category} = proxy.useDict('document_status', 'treatment_category');

const immunizationList = ref([]);
const immunizationOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const isExpandAll = ref(true);
const refreshTable = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    title: null,
    treatmentCategory: null,
  },
  rules: {
    title: [
      {required: true, message: "标题不能为空", trigger: "blur"}
    ],
    treatmentCategory: [
      {required: true, message: "治疗类别不能为空", trigger: "change"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询治疗免疫列表 */
function getList() {
  loading.value = true;
  listImmunization(queryParams.value).then(response => {
    immunizationList.value = proxy.handleTree(response.data, "id", "parentId");
    loading.value = false;
  });
}

/** 查询治疗免疫下拉树结构 */
function getTreeselect() {
  listImmunization().then(response => {
    immunizationOptions.value = [];
    const data = {id: 0, title: '顶级节点', children: []};
    data.children = proxy.handleTree(response.data, "id", "parentId");
    immunizationOptions.value.push(data);
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    parentId: null,
    title: null,
    treatmentCategory: null,
    henBatch: null,
    ageInDays: null,
    chickenCount: null,
    drugCost: null,
    operationTime: null,
    description: null,
    reviewStatus: null,
    reviewTime: null
  };
  proxy.resetForm("immunizationRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  if (row != null && row.id) {
    form.value.parentId = row.id;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "添加治疗免疫";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  await getTreeselect();
  if (row != null) {
    form.value.parentId = row.parentId;
  }
  getImmunization(row.id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改治疗免疫";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["immunizationRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateImmunization(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addImmunization(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除治疗免疫编号为"' + row.id + '"的数据项？').then(function () {
    return delImmunization(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

getList();
</script>
